//
//  seachResultViewController.m
//  dataGathering
//
//  Created by apple on 14/12/8.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "seachResultViewController.h"
#import "collectedDataDetailView.h"

#import "IMQuickSearch.h"

@interface seachResultViewController ()
@property (nonatomic, strong) UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *searchResultContainerView;

@property(nonatomic, assign)NSInteger selectedIndex;
@property(nonatomic, retain)collectedData *selectedData;


@property (weak, nonatomic) IBOutlet UITextField *searchTextField;

@property (nonatomic, retain) IMQuickSearch *QuickSearch;
@property (nonatomic, assign) NSInteger currentMainType;
@property (nonatomic, retain) DOPDropDownMenu *dropMenu;

@property(nonatomic, retain)LocalFeatureModel *featureModel;

@end

@implementation seachResultViewController

BOOL show;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _searchTextField.delegate = self;
    
    _featureModel = [[currentWorkSetManager sharedInstance] getLocalFeatureModel];
    
    ((searchResultTableViewController*)[[self childViewControllers] objectAtIndex:0]).searchResultList = _searchResultList;
    
    ((searchResultTableViewController*)[[self childViewControllers] objectAtIndex:0]).selectProtocol = self;
    
    // Do any additional setup after loading the view.
    _dropMenu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(self.view.bounds.origin.x, self.view.bounds.origin.y+64)  andHeight:48 :CGSizeMake(200, 600)];
    
    _dropMenu.dataSource = self;
    _dropMenu.delegate = self;
    show = NO;
    [self.view addSubview:_dropMenu];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue identifier] && [[segue identifier] compare:@"toCollectedDataDetailView"] == NSOrderedSame){
        
        ((collectedDataDetailView*)[segue destinationViewController]).currentData = _selectedData;
        ((collectedDataDetailView*)[segue destinationViewController]).index = _selectedIndex;
        
    } else if([segue identifier] && [[segue identifier] compare:@"toSelectedDataDetailView"] == NSOrderedSame){
        
    }
}

- (IBAction)backButtonClicked:(id)sender {
    [((UINavigationController*)(self.parentViewController)) popViewControllerAnimated:YES];
}

- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu {
    return 1;
}

- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column {
    return ([[_featureModel groups] count] +1);
}

- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath {

    if([indexPath row] == 0){
        return @"全部结果";
    } else {
        return [(LocalGroupModel*)[[_featureModel groups] objectAtIndex:([indexPath row]-1)] name];
    }
    
}

-(void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if([indexPath row] == 0){
        ((searchResultTableViewController*)[[self childViewControllers] objectAtIndex:0]).searchResultList = _searchResultList;
        [((searchResultTableViewController*)[[self childViewControllers] objectAtIndex:0]).tableView reloadData];
        return;
    }
    _currentMainType = [indexPath row] - 1;
    
    if(!show){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x+200, self.view.bounds.origin.y+64+48, 0, 600) style:UITableViewStylePlain];
        _tableView.rowHeight = 48;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        
        [self.view addSubview:_tableView];
        
        [UIView animateWithDuration:0.2 animations:^{
            _tableView.frame = CGRectMake(self.view.bounds.origin.x+200, self.view.bounds.origin.y+64+48, 282, 600);
        }];
        show = YES;
    } else {
        [_tableView reloadData];
    }
}

-(void)menu:(DOPDropDownMenu *)menu didTapTitle:(NSInteger)test
{
    if(_tableView && ![_tableView isHidden]){
        [UIView animateWithDuration:0.2 animations:^{
            _tableView.frame = CGRectMake(self.view.bounds.origin.x+200, self.view.bounds.origin.y+64+48, 0, 600);
        } completion:^(BOOL finished) {
            [_tableView removeFromSuperview];
            _tableView = nil;
            show = NO;
        }];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ([[(LocalGroupModel*)[[_featureModel groups] objectAtIndex:_currentMainType] layers] count]+1);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"DropDownSubMenuCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    if([indexPath row] == 0){
        cell.textLabel.text = @"全部结果";
    } else {
        
        cell.textLabel.text = [(LocalLayerModel*)[[(LocalGroupModel*)[[_featureModel groups] objectAtIndex:_currentMainType] layers] objectAtIndex:([indexPath row]-1)] name];
        
        //cell.textLabel.text = [_subTypeList objectAtIndex:([indexPath row]-1)];
    }

    cell.backgroundColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    [cell.textLabel setTextColor:[UIColor colorWithRed:151.0/255 green:151.0/255 blue:151.0/255 alpha:1]];
    cell.separatorInset = UIEdgeInsetsZero;
    
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == 0){
        NSString *mainType = [(LocalGroupModel*)[[_featureModel groups] objectAtIndex:_currentMainType] name];
        IMQuickSearchFilter *searchTypeFilter = [IMQuickSearchFilter filterWithSearchArray:_searchResultList keys:@[@"mainType"]];
        
        self.QuickSearch = [[IMQuickSearch alloc] initWithFilters:@[searchTypeFilter]];
        NSArray *filteredResults = [self.QuickSearch filteredObjectsWithValue:mainType];
        
        ((searchResultTableViewController*)[[self childViewControllers] objectAtIndex:0]).searchResultList = filteredResults;
        [((searchResultTableViewController*)[[self childViewControllers] objectAtIndex:0]).tableView reloadData];
        
    } else {
        
        NSString *selectSubType = [(LocalLayerModel*)[[(LocalGroupModel*)[[_featureModel groups] objectAtIndex:_currentMainType] layers] objectAtIndex:([indexPath row]-1)] name];
        
        IMQuickSearchFilter *searchTypeFilter = [IMQuickSearchFilter filterWithSearchArray:_searchResultList keys:@[@"type"]];
        
        
        self.QuickSearch = [[IMQuickSearch alloc] initWithFilters:@[searchTypeFilter]];
        NSArray *filteredResults = [self.QuickSearch filteredObjectsWithValue:selectSubType];
        
        ((searchResultTableViewController*)[[self childViewControllers] objectAtIndex:0]).searchResultList = filteredResults;
        [((searchResultTableViewController*)[[self childViewControllers] objectAtIndex:0]).tableView reloadData];
    }
    [_dropMenu closeMenu];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if([[textField text] compare:@""] != NSOrderedSame){
        
        [self.QuickSearch removeAllFilters];
        
        
        IMQuickSearchFilter *searchTypeFilter = [IMQuickSearchFilter filterWithSearchArray:_searchResultList keys:@[@"name", @"idNumber"]];
        
        
        self.QuickSearch = [[IMQuickSearch alloc] initWithFilters:@[searchTypeFilter]];
        NSArray *filteredResults = [self.QuickSearch filteredObjectsWithValue:[textField text]];
        
        ((searchResultTableViewController*)[[self childViewControllers] objectAtIndex:0]).searchResultList = filteredResults;
        [((searchResultTableViewController*)[[self childViewControllers] objectAtIndex:0]).tableView reloadData];
        
    }
    return YES;
}

- (void)callSearchResultDataDetailView:(NSInteger)index :(collectedData *)selectedData
{
    _selectedIndex = index;
    _selectedData = selectedData;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showSelectedData" object:selectedData];
    [self performSegueWithIdentifier:@"toCollectedDataDetailView" sender:nil];
}


@end
